package work.mediway.mdm.biz.dataQuality;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mediway.hos.common.utils.spring.SpringUtils;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import work.mediway.mdm.biz.service.biz.SsDataQualityConfigItemService;
import work.mediway.mdm.biz.service.biz.SsDataQualityLogItemService;
import work.mediway.mdm.core.constant.MdmConstant;
import work.mediway.mdm.core.enums.StatusWhetherEnum;
import work.mediway.mdm.core.exception.DataException;
import work.mediway.mdm.biz.dto.biz.DataQualityDataExceptionDealDTO;
import work.mediway.mdm.biz.entity.biz.SsDataQualityConfig;
import work.mediway.mdm.biz.entity.biz.SsDataQualityConfigItem;
import work.mediway.mdm.biz.entity.biz.SsDataQualityLogItem;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2022/8/10
 */
@Slf4j
public class DataQualityDataDeleteExceptionDealStrategyImpl implements DataQualityDataExceptionDealStrategy {
    @Override
    public void dealDataQualityExceptionValue(DataQualityDataExceptionDealDTO exceptionDealDTO) {
        log.info(exceptionDealDTO.getLogItemId() + ": DELETE");
        SsDataQualityConfig dataQualityConfig = exceptionDealDTO.getDataQualityConfig();
        // 查询需要处理的数据唯一键值
        SsDataQualityLogItem dataQualityLogItem = SpringUtils.getBean(SsDataQualityLogItemService.class).getById(exceptionDealDTO.getLogItemId());
        // 查询配置字段信息
        List<SsDataQualityConfigItem> dataQualityConfigItems = SpringUtils.getBean(SsDataQualityConfigItemService.class)
                .list(Wrappers.lambdaQuery(SsDataQualityConfigItem.class).eq(SsDataQualityConfigItem::getDataQualityConfigId, dataQualityConfig.getId()));
        if (CollUtil.isNotEmpty(dataQualityConfigItems)) {
            // 唯一值配置
            List<SsDataQualityConfigItem> uniqueItems = dataQualityConfigItems.stream().filter(item -> StrUtil.equals(item.getUniqueFlag(), StatusWhetherEnum.STATUS_Y.getStatusCode())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(uniqueItems)) {
                String logUnique = dataQualityLogItem.getLogUnique();
                if (StrUtil.equalsAnyIgnoreCase(dataQualityConfig.getTargetSchemaName(), MdmConstant.SCHEMA_NAME)) {
                    logUnique = "00001_" + logUnique;
                }
                String sql = StrUtil.strBuilder().append("DELETE ")
                        .append(" FROM ")
                        .append(StrUtil.isBlank(dataQualityConfig.getTargetSchemaName()) ? dataQualityConfig.getTargetTableCode() : dataQualityConfig.getTargetSchemaName() + StrPool.DOT + dataQualityConfig.getTargetTableCode())
                        .append(" WHERE ").append(uniqueItems.get(0).getTargetSystemField()).append(" IN (").append("'").append(logUnique).append("'").append(" )")
                        .toString();
                // TODO: 2022/10/6
                /*try {
                    log.info("删除目标系统数据, 对应SQL为: {}", sql);
                    SqlExecutor.execute(SpringUtils.getBean(BaseDatabaseHelper.class).getConnectionByDatabaseId(Long.valueOf(dataQualityConfig.getTargetAccessSystemId())), sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new BizException(StrUtil.format("删除目标系统数据, 对应SQL为: {}", sql), e);
                }*/
            }
        } else {
            throw new DataException(StrUtil.format("未查询到对应的配置信息, LogItemId = {}", exceptionDealDTO.getLogItemId()));
        }
    }
}
